﻿Public Class MASCOUNTRY
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub codeCountryTextbox_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = True
        End If
    End Sub

    Private Sub MASCOUNTRY_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        selectDataGridCountry()

        DesignForm(Me)
        DesignGrid(MASCOUNTRYGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))

        connect.Dispose()
    End Sub

    Private Sub selectDataGridCountry()
        Dim objSelectDatamasCountry As New prated
        Dim MASCOUNTRY As DataTable
        MASCOUNTRY = connect.GetTable(objSelectDatamasCountry.countrySelectCountry_)
        MASCOUNTRYGrid.PrimaryGrid.DataSource = MASCOUNTRY
    End Sub

    Private Sub MASCOUNTRYGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASCOUNTRYGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสประเทศ").Value() IsNot DBNull.Value Then
            codeCountryTextbox.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสประเทศ").Value()
        Else
            codeCountryTextbox.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value() IsNot DBNull.Value Then
            countryTextbox.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value()
        Else
            countryTextbox.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("country").Value() IsNot DBNull.Value Then
            txtCountry_en.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("country").Value()
        Else
            txtCountry_en.Text = ""
        End If

        addCountry.Enabled = False
    End Sub

    Private Sub addCountry_Click(sender As Object, e As EventArgs) Handles addCountry.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If codeCountryTextbox.Text.Length > 0 And countryTextbox.Text.Length > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New prated
                    objInsertData.countryCODECOUNTRY_ = CInt(codeCountryTextbox.Text)
                    objInsertData.countryCOUNTRY_ = countryTextbox.Text
                    objInsertData.countryCOUNTRY_EN_ = txtCountry_en.Text
                    objInsertData.insertCountry()

                    selectDataGridCountry()

                    'connectlocal.Dispose()
                    codeCountryTextbox.Text = ""
                    countryTextbox.Text = ""
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editCountry_Click(sender As Object, e As EventArgs) Handles editCountry.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If codeCountryTextbox.Text.Length > 0 And countryTextbox.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขประเทศดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New prated
                        objEditData.countryCODECOUNTRY_ = CInt(codeCountryTextbox.Text)
                        objEditData.countryCOUNTRY_ = countryTextbox.Text
                        objEditData.countryCOUNTRY_EN_ = txtCountry_en.Text
                        objEditData.editCountry()

                        selectDataGridCountry()

                        'connectlocal.Dispose()

                        codeCountryTextbox.Text = ""
                        countryTextbox.Text = ""
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
                addCountry.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub clearForm()
        codeCountryTextbox.Clear()
        countryTextbox.Clear()
        txtCountry_en.Clear()
        selectDataGridCountry()
        addCountry.Enabled = True
        editCountry.Enabled = True
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("mascountry")
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("mascountry")
        nextform.ShowDialog()
    End Sub

    Private Sub btnClearForm_Click(sender As Object, e As EventArgs) Handles btnClearForm.Click
        clearForm()
    End Sub
End Class